<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page isELIgnored="false" %>
<%
    String path = request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>李宁体育</title>
    <link rel="shortcut icon" href="favicon.ico">
    <link href="<%=path%>/css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet">
    <link href="<%=path%>/css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
    <link href="<%=path%>/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link href="<%=path%>/css/style.min862f.css?v=4.1.0" rel="stylesheet">
    <link href="<%=path%>/css/plugins/chosen/chosen.css" rel="stylesheet">
    <link href="<%=path%>/css/plugins/ionRangeSlider/ion.rangeSlider.css" rel="stylesheet">
    <link href="<%=path%>/css/plugins/ionRangeSlider/ion.rangeSlider.skinFlat.css" rel="stylesheet">
    <link href="<%=path%>/css/plugins/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css"
          rel="stylesheet">
    <link href="<%=path%>/css/animate.min.css" rel="stylesheet">
    <link href="<%=path%>/css/style.min862f.css?v=4.1.0" rel="stylesheet">
    <link href="<%=path%>/css/plugins/pines-notify/jquery.pnotify.default.css" rel="stylesheet">
    
</head>
<body class="gray-bg">
	<div class="wrapper wrapper-content animated fadeInRight">
	    <div class="row">
	        <div class="col-sm-12">
	            <div class="ibox float-e-margins">
	                <div class="ibox-title">
	                    <h5>新增礼券配置</h5>                
	                </div>
	                <div class="ibox-content">
	                    <form class="form-horizontal m-t" id="giftConfigInsertOrUpdateForm" action="saveOrUpdateGiftConfig" method="post"  enctype="multipart/form-data">
                    		<input type="hidden" id="id" name="id" value="${id}" />
	                    	<input type="hidden" id="mpId" name="mpId" value="${mpId}" />
	                    	
	                    	<c:if test="${empty id}">
	                    	
	                    		<div class="form-group">
									<label class="col-sm-2 control-label">类型</label><font color="red">*</font>
									<div class="col-sm-3">
										<select class="form-control m-b" name="type" id="type">
											<option value="1">新人礼</option>
											<option value="2">感恩礼</option>
										</select>
									</div>
								</div>
								
								<div class="form-group">
									<label class="col-sm-2 control-label">奖品名称</label><font color="red">*</font>
									<div class="col-sm-3">
										<input type="text" class="form-control" name="name" id="name" maxlength="64"></input>
									</div>
								</div>
								
								<div class="form-group">
									<label class="col-sm-2 control-label">奖品库存</label><font color="red">*</font>
									<div class="col-sm-3">
										<input type="text" class="form-control" name="totalQty" id="totalQty" maxlength="10"></input>
									</div>
								</div>
								
								<div class="form-group">
									<label class="col-sm-2 control-label">中奖概率</label>%<font color="red">*</font>
									<div class="col-sm-3">
										<input type="text" class="form-control" name="probability" id="probability" maxlength="6" />
									</div>
								</div>
								
								<div class="form-group">
									<label class="col-sm-2 control-label">奖品类型</label><font color="red">*</font>
									<div class="col-sm-3">
									   <select class="form-control m-b" name="prizeType" id="prizeType">
											<option value="coupon">门店优惠券</option>
											<option value="point">积分</option>
											<option value="product">实物</option>
										</select>
									</div>
								</div>
								
								<div id="couponTemplateIdDiv" class="form-group">
									<label class="col-sm-2 control-label">优惠券模板ID</label><font color="red">*</font>
									<div class="col-sm-3">
										<select class="chosen-select form-control" name="couponTemplateId" id="couponTemplateId">
											<option value="">全部</option> 
											<c:forEach items="${wxCardConfigList}" var="wxCardConfig">
												<option value="${wxCardConfig.templateId}">${wxCardConfig.name}</option>
											</c:forEach>
										</select>
									</div>
								</div>
								<div id="pointsDiv" style="display:none;" class="form-group">
									<label class="col-sm-2 control-label">积分值</label><font color="red">*</font>
									<div class="col-sm-3">
										<input type="text" class="form-control" name="points" id="points" disabled="disabled"></input>
									</div>
								</div>
								<div id="pictureDiv" style="display:none;" class="form-group">
									<label class="col-sm-2 control-label">商品图片</label><font color="red">*</font>
									<div class="col-sm-3">
										<input id="picture" name="picture" type="file" maxlength="32" title="图片" placeholder="图片" disabled="disabled"/> 
									</div>
								</div>
								<div class="form-group">
	                                 <div class="col-sm-4 col-sm-offset-3">
		                                  <button class="btn btn-primary" type="button" onclick="subFormGiftConfig();">保 存</button>
		                                  &nbsp;<button class="btn btn-info" type="button" onclick="goBackGiftConfigList()">返  回</button>
	                                 </div>
	                            </div>
	                    	</c:if>
	                    	<c:if test="${not empty id}">
	                    		<div class="form-group">
									<label class="col-sm-2 control-label">类型</label><font color="red">*</font>
									<div class="col-sm-3">
										<select class="form-control m-b" name="type" id="type">
											<option value="1" <c:if test="${giftConfig.type == 1}">selected</c:if>>新人礼</option>
											<option value="2" <c:if test="${giftConfig.type == 2}">selected</c:if>>感恩礼</option>
										</select>
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">奖品名称</label><font color="red">*</font>
									<div class="col-sm-3">
										<input type="text" class="form-control" name="name" id="name" value="${giftConfig.name}"></input>
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">奖品库存</label><font color="red">*</font>
									<div class="col-sm-3">
										<input type="text" class="form-control" name="totalQty" id="totalQty" value="${giftConfig.totalQty}"></input>
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">中奖概率</label>%<font color="red">*</font>
									<div class="col-sm-3">
										<input type="text" class="form-control" name="probability" id="probability" value="${giftConfig.probability}" />
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">奖品类型</label><font color="red">*</font>
									<div class="col-sm-3">
									   <select class="form-control m-b" name="prizeType" id="prizeType">
											<option value="coupon" <c:if test="${giftConfig.prizeType == 'coupon'}">selected</c:if>>门店优惠券</option>
											<option value="point" <c:if test="${giftConfig.prizeType == 'point'}">selected</c:if>>积分</option>
											<option value="product" <c:if test="${giftConfig.prizeType == 'product'}">selected</c:if>>实物</option>
										</select>
									</div>
								</div>
								
								<div id="couponTemplateIdDiv" class="form-group">
									<label class="col-sm-2 control-label">优惠券模板ID</label><font color="red">*</font>
									<div class="col-sm-3">
										<select class="chosen-select form-control" name="couponTemplateId" id="couponTemplateId">
											<option value="">全部</option> 
											<c:forEach items="${wxCardConfigList}" var="wxCardConfig">
												<option value="${wxCardConfig.templateId}" <c:if test="${giftConfig.couponTemplateId == wxCardConfig.templateId}">selected</c:if>>${wxCardConfig.name}</option>
											</c:forEach>
										</select>
									</div>
								</div>
								<div id="pointsDiv" class="form-group">
									<label class="col-sm-2 control-label">积分值</label><font color="red">*</font>
									<div class="col-sm-3">
										<input type="text" class="form-control" name="points" id="points" value="${giftConfig.points}"></input>
									</div>
								</div>
								
								<div id="pictureDiv" class="form-group">
									<label class="col-sm-2 control-label">商品图片</label><font color="red">*</font>
									<div class="col-sm-3">
										<input id="picture" name="picture" type="file" maxlength="200" title="图片" placeholder="图片"/> 
									</div>
								</div>
								<div class="form-group">
	                                 <div class="col-sm-4 col-sm-offset-3">
		                                  <button class="btn btn-primary" type="button" onclick="subFormGiftConfig();">更  新</button>
		                                  &nbsp;<button class="btn btn-info" type="button" onclick="goBackGiftConfigList()">返  回</button>
	                                 </div>
	                            </div>
	                    	</c:if>
	                    </form>
	                </div>
	            </div>
	        </div>
	    </div>
	</div>
	<script src="<%=path%>/js/jquery.min.js?v=2.1.4"></script>
	<script src="<%=path%>/js/bootstrapValidator.min.js"></script>
	<!--下拉框-->
	<script src="<%=path%>/js/plugins/chosen/chosen.jquery.js"></script>	
	<script src="<%=path%>/js/plugins/pines-notify/jquery.pnotify.js"></script>
	<script src="<%=path%>/js/common.js"></script>
	<script >
	 //表单验证
    var config = {
            ".chosen-select": {},
            ".chosen-select-deselect": {allow_single_deselect: !0},
            ".chosen-select-no-single": {disable_search_threshold: 10},
            ".chosen-select-no-results": {no_results_text: "Oops, nothing found!"},
            ".chosen-select-width": {width: "95%"}
    };
  
    for (var selector in config)$(selector).chosen(config[selector]);
			var giftId = '${id}';
			var errCode = '${errCode}';
			$(document).ready(function(){
				if(errCode == '-1'){

					_showInfoMessage("设置概率不能大于100%。", 'error');
				}
			　if("${not empty id}" == "true") {
					if("${giftConfig.prizeType}" == "coupon") {
						$("#couponTemplateIdDiv").css("display","block");
						$("#couponTemplateId").removeAttr("disabled");
						$("#pointsDiv").css("display","none");
						$("#points").attr("disabled","disabled");
						$("#pictureDiv").css("display","none");
					}else if("${giftConfig.prizeType}" == "point") {
						$("#pointsDiv").css("display","block");
						$("#points").removeAttr("disabled");
						$("#couponTemplateIdDiv").css("display","none");
						$("#couponTemplateId").attr("disabled","disabled");
						$("#pictureDiv").css("display","none");
					} else if("${giftConfig.prizeType}" == "product") {
						$("#couponTemplateIdDiv").css("display","none");
						$("#couponTemplateId").attr("disabled","disabled");
						$("#pointsDiv").css("display","none");
						$("#points").attr("disabled","disabled");
					}
			　}
			$("#type").change(function() {
				var prizeType = $("#prizeType");
				if(this.value == "1") {					//新人礼
					prizeType.html("");
					prizeType.append("<option value='coupon''>门店优惠券</option><option value='point'>积分</option><option value='product'>实物</option>")
				} else if(this.value == "2") {			//感恩礼
					prizeType.html("");
					prizeType.append("<option value='coupon''>门店优惠券</option><option value='point'>积分</option>")
				}
			})
			$("#prizeType").change(function(){
				if(this.value == "coupon") {
					$("#couponTemplateIdDiv").css("display","block");
					$("#couponTemplateId").removeAttr("disabled");
					$("#pointsDiv").css("display","none");
					$("#points").attr("disabled","disabled");
					$("#pictureDiv").css("display","none");
					$("#picture").attr("disabled","disabled");
				} else if(this.value == "point") {
					$("#pointsDiv").css("display","block");
					$("#points").removeAttr("disabled");
					$("#couponTemplateIdDiv").css("display","none");
					$("#couponTemplateId").attr("disabled","disabled");
					$("#pictureDiv").css("display","none");
					$("#picture").attr("disabled","disabled");
				} else if(this.value == "product") {
					$("#couponTemplateIdDiv").css("display","none");
					$("#couponTemplateId").attr("disabled","disabled");
					$("#pointsDiv").css("display","none");
					$("#points").attr("disabled","disabled");
					$("#pictureDiv").css("display","block");
					$("#picture").removeAttr("disabled");
				}
			})
			}); 
		
	    function goBackGiftConfigList() {
	        window.location.href="<%=path%>/giftCertificate/queryGiftConfigList";
		}
		function subFormGiftConfig() {
			var totalQtyOrPointsRxg = /^\+?[1-9][0-9]*$/;
			var probabilityRxg = /^\d\.([1-9]{1,2}|[0-9][1-9])$|^[1-9]\d{0,1}(\.\d{1,2}){0,1}$|^100(\.0{1,2}){0,1}$/;
			var type = $("#type").val();
			var name = $("#name").val();
			var totalQty = $("#totalQty").val();
			var probability = $("#probability").val();
			var prizeType = $("#prizeType").val();
			var couponTemplateId = $("#couponTemplateId").val();
			var points = $("#points").val();
			var picture = $("#picture").val();
			if (type == '' || type == null) {
				_showInfoMessage("类型不能为空", 'error');
				return false;
			}
			if (name == '' || name == null) {
				_showInfoMessage("奖品名称不能为空", 'error');
				return false;
			}
			if (totalQty == '' || totalQty == null) {
				_showInfoMessage("奖品库存不能为空", 'error');
				return false;
			} else if(!totalQtyOrPointsRxg.test(totalQty)) {
				_showInfoMessage("请输入正确的奖品库存", 'error');
				return false;
			}
			if (probability == '' || probability == null) {
				_showInfoMessage("中奖概率不能为空", 'error');
				return false;
			} else if(!probabilityRxg.test(probability)) {
				_showInfoMessage("请输入正确的中奖概率", 'error');
				return false;
			}
			if (prizeType == '' || prizeType == null) {
				_showInfoMessage("奖品类型不能为空", 'error');
				return false;
			} else {
				if(prizeType == "coupon") {
					if(couponTemplateId == '' || couponTemplateId == null) {
						_showInfoMessage("优惠券模板ID不能为空", 'error');
						return false;
					}
				} else if(prizeType == "point"){
					if(points == '' || points == null) {
						_showInfoMessage("积分值不能为空", 'error');
						return false;
					} else if(!totalQtyOrPointsRxg.test(points)) {
						_showInfoMessage("请输入正确的积分值", 'error');
						return false;
					}
				} else if(prizeType == "product") {
					if (picture == '' || picture == null) {
						if(giftId == ''){
							_showInfoMessage("请上传奖品图片", 'error');	
							return false;						
						}
					}
				}
			}
			
			$("#giftConfigInsertOrUpdateForm").submit();
		}
	</script>
</body>
</html>